package com.tianbo.elfii.client.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

public class WlanPwConnectDialog extends JPanel implements DocumentListener{

	public WlanAP connectingAP;
	
	public JButton confirmBtn;
	public JButton cancelBtn;
	
    BufferedImage bgImg = null;
	
    public JLabel titleLabel = null;
    
    public JTextField pwInputField;
    public JTextField textField;
    public JPasswordField passwordField;
    
    public JCheckBox pwVisibilityCheckBox;
    public ImageIcon pwVisibleImg = null;
    public ImageIcon pwUnvisibleImg = null;
    
    public String titleStr = "SSID";
    public String apQualityStr = "信号强度      ";
    public String apEncryptTypeStr = "安全类型      ";
    public String apPwStr = " 密 码";
    public String pwVisibility = "显示密码";
    
    public String pwNotMatchStr = "密码不正确";
    public String otherFailedStr = "连接失败 请重试";
    
    public boolean isError = true;
    
    public Font titleStrFont;
    
    public ElfiiCommonKeyboard keyboard = null;
    
	public WlanPwConnectDialog(){
		
		titleStrFont = new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,24);
	}
	
	public WlanPwConnectDialog(WlanAP connectingAP){
		this.connectingAP = connectingAP;
		if(connectingAP!=null){
			titleStr = connectingAP.getSSID();
			
			int quality = connectingAP.getQuality();
			
			if(quality<25){
				apQualityStr+="弱";
			}
			else if(quality>=25 && quality<50){
				apQualityStr+="较弱";
			}
			else if(quality>=50 && quality<75){
				apQualityStr+="较强";
			}
			else{
				apQualityStr+="强";
			}
			
			if(connectingAP.getEncryptedStat()){
				if(connectingAP.getEncryptType().equals("5")){
					apEncryptTypeStr += "WPA/WPA-WPA2";
				}
				else{
					apEncryptTypeStr += "WEP";
				}
			}
			else{
				apEncryptTypeStr += "无";
			}
		}
		
		titleStrFont = new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,24);
	}
	
	public void init(){
		this.setLayout(null);
		initImg();
		initBtn();
		initCheckBox();
		initInputField(isError,"");
	}
	
	public void init(boolean isError,String errorPw){
		
		this.setLayout(null);
		initImg();
		initBtn();
		initCheckBox();
		initInputField(isError,errorPw);
		
	}

	public void initImg(){
        File f;
        try {
            f=new File("resource-common/images/config/wlan/ss_03.png");
            bgImg=ImageIO.read(f);
            
            f=new File("resource-common/images/config/ss_06.png");
            pwUnvisibleImg = new ImageIcon(ImageIO.read(f));
            
            f=new File("resource-common/images/config/ss_07.png");
            pwVisibleImg = new ImageIcon(ImageIO.read(f));
            
        } catch(Exception e) {System.out.print(e);}
	}
	
	public void initBtn(){
		confirmBtn = new JButton();
		cancelBtn = new JButton();
		
		confirmBtn.setIcon(new ImageIcon("resource-common/images/login/sssss06.png"));
		cancelBtn.setIcon(new ImageIcon("resource-common/images/login/sssss07.png"));
		
		cancelBtn.setContentAreaFilled(false);
		cancelBtn.setBorder(null);
		cancelBtn.setFocusPainted(false);
		
		confirmBtn.setContentAreaFilled(false);
		confirmBtn.setBorder(null);
		confirmBtn.setFocusPainted(false);

		confirmBtn.setBounds(100, 220, 141, 44);
		cancelBtn.setBounds(304, 220, 141, 44);
		
		this.add(confirmBtn);
		this.add(cancelBtn);
		
		confirmBtn.setEnabled(false);
	}
	
	public void initInputField(boolean isError,String errorPw){
		
		this.isError = isError;
		
		textField = new JTextField(20);
		passwordField = new JPasswordField(20);
		
		this.add(textField);
		
		this.add(passwordField);

		textField.setBorder(null);
		textField.setBounds(185, 155, 200, 20);
		textField.setVisible(false);
		
		passwordField.setBorder(null);
		passwordField.setBounds(185, 155, 200, 20);
		passwordField.setVisible(false);
		
		if(pwVisibilityCheckBox.isSelected()){
			pwInputField = textField;
		}
		else{
			pwInputField = passwordField;
		}
		
		pwInputField.setVisible(true);
		pwInputField.setFocusable(true);
		
		if(!isError){
			pwInputField.setBorder(BorderFactory.createLineBorder(ElfiiCommonConstant.Elfii_Red));
			pwInputField.setText(errorPw);
		}

		if(connectingAP.getCurStat() == APSTAT.SAVED){
			pwInputField.setText(connectingAP.getPassword());
			confirmBtn.setEnabled(true);
		}
		
		textField.getDocument().addDocumentListener(this);
		passwordField.getDocument().addDocumentListener(this);
	}
	
	public void initCheckBox(){
		
		pwVisibilityCheckBox = new JCheckBox();
		pwVisibilityCheckBox.setIcon(pwUnvisibleImg);
		
		pwVisibilityCheckBox.setContentAreaFilled(false);
		pwVisibilityCheckBox.setRolloverEnabled(false);
		this.add(pwVisibilityCheckBox);
		pwVisibilityCheckBox.setBounds(182, 180, 80, 20);
		
		pwVisibilityCheckBox.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				
				boolean tmpFlag = isError;
				
				String pw = pwInputField.getText();
				pwInputField.setVisible(false);
				
				if(pwVisibilityCheckBox.isSelected()){
					pwVisibilityCheckBox.setIcon(pwVisibleImg);
					pwInputField = textField;
				}
				else{
					pwVisibilityCheckBox.setIcon(pwUnvisibleImg);
					pwInputField = passwordField;
				}
				

				
				if(keyboard!=null){
					keyboard.setTextField(pwInputField, 20);
				}
				
				pwInputField.setVisible(true);
				pwInputField.setText(pw);
				pwInputField.setFocusable(true);
				
				if(!tmpFlag){
					pwInputField.setBorder(BorderFactory.createLineBorder(ElfiiCommonConstant.Elfii_Red));
					isError = tmpFlag;
					repaint();
				}
				
			}
			
		});
	}
	
	public void setRelatedKeyboard(ElfiiCommonKeyboard keyboard){
		this.keyboard = keyboard;
	}
	
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
      
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(bgImg,null,0,0);

        g2.setColor(Color.white);
        
        g2.setFont(titleStrFont);
    
        g2.drawString(titleStr, 100, 55);
        
        g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,14));
        
        g2.drawString(apQualityStr, 80, 110);
        g2.drawString(apEncryptTypeStr, 80, 140);
        g2.drawString(apPwStr, 80, 170);
        
        g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,12));
        g2.drawString(pwVisibility,205,195);
        
        if(connectingAP.connectRetFlag == 3){
            g2.setColor(ElfiiCommonConstant.Elfii_Red);
            g2.drawString(pwNotMatchStr,400,170);
        }
        else if(connectingAP.connectRetFlag != -1){
            g2.setColor(ElfiiCommonConstant.Elfii_Red);
            g2.drawString(otherFailedStr,400,170);
        }
    }


	@Override
	public void insertUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		if(!isError){
			isError = true;
			connectingAP.connectRetFlag = -1;
			pwInputField.setBorder(null);
			this.repaint();
		}
		
		if(pwInputField.getText().trim().equals("")){
			confirmBtn.setEnabled(false);
		}
		else{
			confirmBtn.setEnabled(true);
		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		if(!isError){
			isError = true;
			connectingAP.connectRetFlag = -1;
			pwInputField.setBorder(null);
			this.repaint();
		}
		
		if(pwInputField.getText().trim().equals("")){
			confirmBtn.setEnabled(false);
		}
		else{
			confirmBtn.setEnabled(true);
		}
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame w = new JFrame();
		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		w.setUndecorated(true); //
		w.setSize(545, 272);
//		w.show();
		WlanPwConnectDialog dialog = new WlanPwConnectDialog();
		dialog.setBounds(0, 0, 545, 272);
		dialog.init(false,"123");
		w.setContentPane(dialog);
		w.show();
	}

}
